package com.my.house.controller;


import com.my.house.common.ApiResult;
import com.my.house.common.utils.ThreadLocalHolder;
import com.my.house.domain.User;
import com.my.house.vo.BuildingReqVO;
import lombok.RequiredArgsConstructor;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import lombok.extern.slf4j.Slf4j;
import com.my.house.domain.Building;
 import com.my.house.service.BuildingService;

import java.util.List;

/**
 * <p>
 * 楼栋信息表 前端控制器
 * </p>
 *
 * @author yangll
 * @since 2025-02-08
 */
@Slf4j
@RestController
@RequestMapping("/api/building")
@RequiredArgsConstructor
public class BuildingController {
    private final BuildingService buildingService;

    @GetMapping("/list")
    public ApiResult<List<Building>> list() {
        log.info("/building/list");
        User user = ThreadLocalHolder.getUser();
        assert user != null;
        return ApiResult.ok(buildingService.findByUserId(user.getId()));
    }

    @PostMapping("addBuilding")
    public ApiResult<Void> addBuilding(@RequestBody @Validated BuildingReqVO reqVO) {
        User user = ThreadLocalHolder.getUser();
        assert user != null;
        buildingService.addBuilding(user.getId(), reqVO.getName(), reqVO.getAddress());
        return ApiResult.ok();
    }

    @PostMapping("editBuilding")
    public ApiResult<Void> editBuilding(@RequestBody @Validated BuildingReqVO reqVO) {
        User user = ThreadLocalHolder.getUser();
        assert user != null;
        Assert.notNull(reqVO.getId(), "id不能为空");
        buildingService.editBuildingById(reqVO.getId(), user.getId(), reqVO.getName(), reqVO.getAddress());
        return ApiResult.ok();
    }

    @PostMapping("delBuilding")
    public ApiResult<Void> delBuilding(@RequestBody Long id) {
        User user = ThreadLocalHolder.getUser();
        assert user != null;
        Assert.notNull(id, "id不能为空");
        buildingService.deleteBuildingById(user.getId(), id);
        return ApiResult.ok();
    }
}
